/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.util;

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 10-12-17
 * Time: 上午11:58
 * 本hashMap没有最大限制，主要使用在IOC中缓存对象
 */
public class LockHashMap<K, V> extends LinkedHashMap<K, V> {
    private Lock readLock;
    private Lock writeLock;

    public LockHashMap() {
        ReadWriteLock globalLock = new ReentrantReadWriteLock();
        readLock = globalLock.readLock();
        writeLock = globalLock.writeLock();
    }

    @Override
    public V get(Object key) {
        readLock.lock();
        try {
            return super.get(key);
        }
        finally {
            readLock.unlock();
        }
    }

    @Override
    public V put(K key, V value) {
        writeLock.lock();
        try {
            return super.put(key, value);
        }
        finally {
            writeLock.unlock();
        }
    }

    @Override
    public Set<K> keySet() {
        readLock.lock();
        try {
            return super.keySet();
        }
        finally {
            readLock.unlock();
        }
    }

    @Override
    public V remove(Object key) {
        writeLock.lock();
        try {
            return super.remove(key);
        }
        finally {
            writeLock.unlock();
        }
    }

}